package com.foreknow.sercive.Impl;

import com.foreknow.dao.Impl.AdminDaoImpl;
import com.foreknow.db.DBManager;
import com.foreknow.model.Admin;
import com.foreknow.sercive.AdminService;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class AdminServiceImpl implements AdminService {
    private AdminDaoImpl dao=new AdminDaoImpl();
    @Override
    public boolean add(Admin admin){
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        dao.setConnection(conn);
        boolean isRight=false;
        try {
            isRight = dao.save(admin);
            conn.commit();
        } catch (SQLException e) {
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        }
        return isRight;
    }

    @Override
    public boolean selectAll(String name,String password) {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        dao.setConnection(conn);
        Admin admin = null;
        try {
            admin = dao.queryAll(name, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (admin == null) {
            return false;
        }
        return true;
    }
}
